<?php
namespace app\classes\helper;
use think\Db;
use think\facade\Env;

class BehaviorRedis
{
    protected $redis;
    protected $host;
    protected $port;
    protected $auth;

    public function __construct()
    {
        $this->host = Env::get('redis.host', 'r-j6cd5qenza064ipy6n.redis.rds.aliyuncs.com');
        $this->port = 6379;
        $this->auth = Env::get('redis.auth', 'dsfweew%$weew2');

        //$this->auth = Env::get('redis.auth', 'r-bp11v3wc1977f8zhaj:dsfweew%$weew2');
    }

    /**
     * 获取reids对象
     * @return \Redis
     */
    public function getRedis()
    {
        if (!is_object($this->redis)) {
            $this->redis = new \Redis();
            $this->redis->connect($this->host, $this->port);
            $this->redis->auth($this->auth);
        }
        return $this->redis;
    }

    /**
     * 添加弹窗记录
     * @param $popId
     * @param $frequency
     * @param $uid
     * @param $expire
     */
    public function addPopLog($popId, $frequency, $uid, $expire)
    {
        $key = "app_pop_{$popId}_{$frequency}_{$uid}";
        $this->getRedis()->set($key, time());
        $this->getRedis()->expire($key, $expire);
    }

    /**
     * 获取弹窗记录
     * @param $popId
     * @param $frequency
     * @param $uid
     * @return bool|mixed|string
     */
    public function getPopLog($popId, $frequency, $uid)
    {
        $key = "app_pop_{$popId}_{$frequency}_{$uid}";
        return $this->getRedis()->get($key);
    }
}
